import axios from 'axios'
export { default as api } from './api'
import { getStorage, getLcuHeader } from '@renderer/utils/utils'
import { client, lcu } from '@renderer/common/constant'

async function request(url, newOptions) {
  const { port } = getStorage(['port'])
  if (url.indexOf(lcu) !== -1) {
    url = `https://127.0.0.1:${port}${url.slice(4, url.length)}`
  }
  if (url.indexOf(client) !== -1) {
    url = `https://127.0.0.1:2999${url.slice(7, url.length)}`
  }
  return axios({
    url,
    ...newOptions
  }).then((response) => {
    if (response.status === 200) {
      return response.data
    } else {
      return null
    }
  })
}

/**
 * get请求
 * @param url
 * @param params
 * @param responseType
 */
export function get(url, params, responseType) {
  const headers = getLcuHeader()
  return request(url, {
    method: 'GET',
    params,
    responseType,
    headers
  })
}

/**
 * post请求
 * @param url
 * @param params
 * @param responseType
 */
export function post(url, params, responseType) {
  const headers = getLcuHeader()
  console.log(headers)
  return request(url, {
    method: 'POST',
    headers,
    responseType,
    data: params
  })
}
